import React, { ReactElement, useState, useEffect } from 'react';
import { View } from 'react-native';

import reportTable from '../../service/reportTable';
import baseTracker from '../../common/baseTracker';
import { AppApi } from '../../api';
import Launch from './components/Launch';

export default (): ReactElement => {
  const [showLaunch, setShowLaunch] = useState<boolean>(false);

  useEffect((): void => {
    reportTable
      .connect()
      .then((): void => {
        reportTable
          .init()
          .then((): void => {
            baseTracker.start(
              (param): Promise<any> => AppApi.report(param),
              reportTable
            );
            setShowLaunch(true);
          })
          .catch((): void => {
            setShowLaunch(true);
          });
      })
      .catch((): void => {
        setShowLaunch(true);
      });
  }, []);

  if (showLaunch) return <Launch />;
  return <View />;
};
